﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Ym01xs")]
    public class Ym01xsController : ApiController
    {

        /// <summary>
        /// /获取提单详情；
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PostYm01xs_Bill_inq")]
        public System.Data.DataSet PostYm01xs_Bill_inq([FromBody]  JObject obj)
        {
            EI.EIInfo inBlock = new EI.EIInfo();
          

            string BILL_OF_LADING_NO = obj["BILL_OF_LADING_NO"] == null ? " " : obj["BILL_OF_LADING_NO"].ToString();


            //提单有效性确认；
            string sql_str = "  SELECT * FROM TSOSD01 WHERE COMPANY_CODE='LT'  " +
                            "   and  BILL_OF_LADING_NO =  '" + BILL_OF_LADING_NO + "' "
                            ;

            EI.EIInfo outBlock = EF.Utility.ExecQuery(sql_str);
            if (outBlock.Tables[0].Rows.Count < 1)
            {
                outBlock.Tables.Add("SYS");
                outBlock.Tables["SYS"].Columns.Add("FLAG");
                outBlock.Tables["SYS"].Columns.Add("MSG");
                outBlock.Tables["SYS"].Rows.Add(-1, "错误，提单【"+ BILL_OF_LADING_NO + "】不存在！");
                return outBlock;
            }
            //测试，暂时去除，不然没有测试数据；
            if (outBlock.Tables[0].Rows[0]["BILL_OF_LADING_STATUS"].ToString().Trim() != "OP")
            {
                outBlock.Tables.Add("SYS");
                outBlock.Tables["SYS"].Columns.Add("FLAG");
                outBlock.Tables["SYS"].Columns.Add("MSG");
                outBlock.Tables["SYS"].Rows.Add(-1, "错误，提单【" + BILL_OF_LADING_NO + "】不是审核状态！");
                return outBlock;
            }


            //TDN2411194
            //已发货重量

            //提单有效性确认；
            sql_str =    "     select nvl(sum(TOTAL_WT), 0) " + 
                         "  FROM tmm0022 WHERE COMPANY_CODE='NM'  " +
                         "     AND BRANCH_CODE = 'N' " +
                         "   and  OUT_STOCK_APPLY_NO =  '" + BILL_OF_LADING_NO + "' "
                            ; 
            EI.EIInfo outBlock_tmm0022 = EF.Utility.ExecQuery(sql_str);
            
            if (  Double.Parse(outBlock_tmm0022.Tables[0].Rows[0][0].ToString())>=
                Double.Parse(outBlock.Tables[0].Rows[0]["TOTAL_WT"].ToString())
                )
            {
                outBlock.Tables.Add("SYS");
                outBlock.Tables["SYS"].Columns.Add("FLAG");
                outBlock.Tables["SYS"].Columns.Add("MSG");
                outBlock.Tables["SYS"].Rows.Add(-1, "错误，提单【" + BILL_OF_LADING_NO 
                    + "】已经出库重量【"+ outBlock_tmm0022.Tables[0].Rows[0][0].ToString() + "】，已无出库数据！");
                return outBlock;
            }







            string sql2  =  " SELECT distinct DELI_STORE_CODE CODE ,"
                            + " DELI_STORE_NAME CODE_DESC_1_CONTENT FROM   "
                            + " TSOSD03 WHERE COMPANY_CODE='LT' "
                            + " and  BILL_OF_LADING_NO =  '" + BILL_OF_LADING_NO + "' "
                            ;
           
            EI.EIInfo outBlock2 = EF.Utility.ExecQuery(sql2);

            //多余1个仓库
            outBlock2.Tables[0].TableName = "STOCK_NO";
            outBlock.Tables.Add(outBlock2.Tables[0].Copy());






            //2该仓库所有的提单信息；
            string sql_str1 = "select   0 MAT_NUM,0 MAT_WT,"; 
            sql_str1 += "    tx.*   from TSOSD03 tx  ";
            sql_str1 += "  where   tx.bill_of_lading_no = '" + BILL_OF_LADING_NO + "' ";
           
            sql_str1 += "   order by tx.BILL_OF_LADING_SUB_NO ";

            EI.EIInfo BInfo_B = EF.Utility.ExecQuery(sql_str1);

            //BInfo_B.Tables[0].Columns.Add("MAT_NO");
            //BInfo_B.Tables[0].Columns.Add("MAT_NUM");
            //BInfo_B.Tables[0].Columns.Add("MAT_WT");

            //BInfo_B.Tables[0].Columns.Add("UNIT_WT_C");//从提单中单出来；

            //BInfo_B.Tables[0].Columns.Add("PURC_STD_DESC");//管捆号；借用
            //BInfo_B.Tables[0].Columns.Add("LOT_NO");
            //BInfo_B.Tables[0].Columns.Add("PONO_NO");



            //提单子项
            BInfo_B.Tables[0].TableName = "Table1";
            outBlock.Tables.Add(BInfo_B.Tables[0].Copy());




        

            //2提单下的未发货信息；
            sql_str1 = "select ta.PONO_NO,tb.OUT_STOCK_NO "
                + " from tmm0011 ta, tmm0022 tb "
                + " where ta.IOSTOCK_BILL_NO = tb.OUT_STOCK_NO "
                + " and ta.company_code = 'LT' and tb.branch_code = 'N' "
                + " and tb.bill_status = '10' ";
       
            sql_str1 += "   and   tb.OUT_STOCK_APPLY_NO = '" + BILL_OF_LADING_NO + "' ";
            sql_str1 += "   order   by ta.iostock_sub_no desc";

            EI.EIInfo BInfo_B2 = EF.Utility.ExecQuery(sql_str1);



            //该提单中已经出库但未保存的信息；
            BInfo_B2.Tables[0].TableName = "Table2";
            outBlock.Tables.Add(BInfo_B2.Tables[0].Copy());
             



            outBlock.Tables[0].TableName = "Table0"; //提单主项；


            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);            
            return outBlock;


        }


        /// <summary>
        /// 获取提单子项对应的材料库位分布【待补充】
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PostYm01xs_Bill_Sub_inq")]
        public System.Data.DataSet PostYm01xs_Bill_Sub_inq([FromBody]  JObject obj)
        {
            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");





            // MAT_NO，PROD_LOT_NO 必须有一个值；钉钉前端校验；
            string BILL_OF_LADING_SUB_NO = obj["BILL_OF_LADING_SUB_NO"] == null ? "" : obj["BILL_OF_LADING_SUB_NO"].ToString();
            string STOCK_NO = obj["STOCK_NO"] == null ? "" : obj["STOCK_NO"].ToString();






            string sql_temp = ""; 
            
            EI.EIInfo Block_TSOSD03 = new EI.EIInfo();


         


          sql_temp = "    select prod_lot_no, LOCATION_NO, count(*) NUM "
                        + "   from TPMMM02N ta, TSOSD03 tb   "
                        + "     WHERE    tb.bill_of_lading_sub_no = '" + BILL_OF_LADING_SUB_NO + "'"
                        + "      and  ta.stock_no = '" + STOCK_NO + "'"
                        + "     and tb.PROD_CODE = ta.PROD_CODE    "
                        + "     and tb.sg_sign = ta.sg_sign "
                        + "     and tb.ORD_SPEC_DESC = ta.mat_spec_desc "
                        + "     and tb.PACK_MODE_CODE = ta.PACK_MODE_CODE "
                        + "     group by  prod_lot_no, LOCATION_NO   "
                        + "     order by prod_lot_no, count(*)  "
                        ;
            Block_TSOSD03 = EF.Utility.ExecQuery(sql_temp);
            if (Block_TSOSD03.Tables[0].Rows.Count < 1)
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "该提单子项[" + BILL_OF_LADING_SUB_NO + "]无有效库存！");
                return outBlock;
            }

            //多余1个仓库
            Block_TSOSD03.Tables[0].TableName = "LOCATION_NO";
            outBlock.Tables.Add(Block_TSOSD03.Tables[0].Copy());


            //string location_no = Block_TSOSD03.Tables[0].Rows[0]["LOCATION_NO"].ToString();

            //for (int i = 1; i < Block_TSOSD03.Tables[0].Rows.Count; i++)
            //{
            //    location_no =  location_no + "/" +  Block_TSOSD03.Tables[0].Rows[i]["LOCATION_NO"].ToString() ;
            //}





            outBlock.Tables["SYS"].Rows.Add(0, "");
            return outBlock;

        }


        /// <summary>
        /// 销售出库，扫描材料
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PostYm01xs_mat_no_inq")]
        public System.Data.DataSet PostYm01xs_mat_no_inq([FromBody]  JObject obj)
        {
            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");





            // MAT_NO，PROD_LOT_NO 必须有一个值；钉钉前端校验；
            string BILL_OF_LADING_NO = obj["BILL_OF_LADING_NO"] == null ? "" : obj["BILL_OF_LADING_NO"].ToString();
            string STOCK_NO = obj["STOCK_NO"] == null ? "" : obj["STOCK_NO"].ToString();
            string MAT_NO = obj["MAT_NO"] == null ? "" : obj["MAT_NO"].ToString();
            string PROD_LOT_NO = obj["PROD_LOT_NO"] == null ? "" : obj["PROD_LOT_NO"].ToString();


            string BILL_OF_LADING_SUB_NO = "";//允许的提单子项；




            string sql_temp = "";
            EI.EIInfo Block_temp = new EI.EIInfo();
            EI.EIInfo Block_TPMMM02N = new EI.EIInfo();
            EI.EIInfo Block_TSOSD03 = new EI.EIInfo();
            EI.EIInfo Block_TMM0001 = new EI.EIInfo();






            if (MAT_NO != "")
            {
                string sql_str0 = " select STATUS,mat_wt,PROD_CODE,PROD_LOT_NO,qual_grade_code,Pack_Mode_Name ";
                sql_str0 += "       ,pack_mode_code,stock_no,mat_spec_desc";
                sql_str0 += "       ,SG_STD,SG_SIGN      ";
                sql_str0 += "       from TPMMM02N                                                                                  ";
                sql_str0 += "       where mat_no = '" + MAT_NO + "' ";

                Block_TPMMM02N = EF.Utility.ExecQuery(sql_str0);
                if (Block_TPMMM02N.Tables[0].Rows.Count < 1)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + MAT_NO + "]不存在！");
                    return outBlock;
                }

                //验证该材料是否满足提单的要求；【仓库、包装方式、质量等级、规格、标准；】
                if (Block_TPMMM02N.Tables[0].Rows[0]["STOCK_NO"].ToString() != STOCK_NO)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + MAT_NO + "]不在库区[" + STOCK_NO + "]！");
                    return outBlock;
                }


                if (Block_TPMMM02N.Tables[0].Rows[0]["STATUS"].ToString() != "19")
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + MAT_NO + "]的状态不满足销售出库要求！");
                    return outBlock;
                }



                //标签根据该信息去找库存，由于老材料共用，故不可根据该标签上的材料号去找库存。
                PROD_LOT_NO = Block_TPMMM02N.Tables[0].Rows[0]["PROD_LOT_NO"].ToString();

                //
                sql_temp = "    select BILL_OF_LADING_SUB_NO "
                           + "    from TSOSD03   "
                           + "     WHERE    BILL_OF_LADING_NO = '" + BILL_OF_LADING_NO + "'"
                           + "     and    PROD_CODE = '" + Block_TPMMM02N.Tables[0].Rows[0]["PROD_CODE"].ToString() + "' "
                           + "     and      SG_SIGN ='" + Block_TPMMM02N.Tables[0].Rows[0]["SG_SIGN"].ToString() + "' "
                           + "      and     ORD_SPEC_DESC ='" + Block_TPMMM02N.Tables[0].Rows[0]["MAT_SPEC_DESC"].ToString().Replace("M", "").Replace("m", "") + "' "
                           + "     and      PACK_MODE_CODE ='" + Block_TPMMM02N.Tables[0].Rows[0]["PACK_MODE_CODE"].ToString() + "' ";

                //非正品，则提单应明确标识；
                if (Block_TPMMM02N.Tables[0].Rows[0]["QUAL_GRADE_CODE"].ToString() != "1")
                {
                    sql_temp = sql_temp + " and QUAL_GRADE_CODE ='" + Block_TPMMM02N.Tables[0].Rows[0]["QUAL_GRADE_CODE"].ToString() + "' ";
                }

                Block_TSOSD03 = EF.Utility.ExecQuery(sql_temp);
                if (Block_TSOSD03.Tables[0].Rows.Count < 1)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + MAT_NO + "]不满足提单要求，请确认品名、钢种、规格、包装方式、质量等级！");
                    return outBlock;
                }

            }
            else //批次管理【老标签，查找满足的提单子项】
            {

                outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + MAT_NO + "]不可为空！");
                return outBlock;
                //批次也是可以的，暂时不考虑；


                sql_temp = "    SELECT BILL_OF_LADING_SUB_NO "
                         + "    from TSOSD03   "
                         + "     WHERE    BILL_OF_LADING_NO = '" + BILL_OF_LADING_NO + "'"
                         + "     AND      (PROD_CODE,SG_SIGN,ORD_SPEC_DESC,PACK_MODE_CODE) in  "
                         + "     AND     (select PROD_CODE,SG_SIGN,MAT_SPEC_DESC,PACK_MODE_CODE  "
                         + "    FROM TMM0001   STOCK_NO= '" + STOCK_NO + "'  "
                         + "    AND LOT_NO ='" + PROD_LOT_NO + "'"
                         + "    AND MAT_NUM  >=1  AND MAT_WT >0 ) ";

                Block_TSOSD03 = EF.Utility.ExecQuery(sql_temp);
                if (Block_TSOSD03.Tables[0].Rows.Count < 1)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "该批次材[" + PROD_LOT_NO + "]的材料不满足提单要求，请确认品名、钢种、规格、包装方式、质量等级！");
                    return outBlock;
                }

            }

            for (int i = 0; i < Block_TSOSD03.Tables[0].Rows.Count; i++)
            {
                BILL_OF_LADING_SUB_NO = BILL_OF_LADING_SUB_NO + Block_TSOSD03.Tables[0].Rows[i]["BILL_OF_LADING_SUB_NO"].ToString() + ",";
            }



            //根据【标签.批次号 + 提单子项.规格 物料主档表找材料及材料是否存在】
            //物料主档：PROD_CODE,SG_SIGN,MAT_SPEC_DESC,PACK_MODE_CODE + STOCK_NO + PROD_LOT_NO 基本唯一了
            sql_temp = " SELECT  ' ' BILL_OF_LADING_SUB_NO " //前台比对需要，记录前台所有可以匹配的提单信息；                    
                        + ",    ' ' SALE_ORDER_SUB_NO "//提单需要的信息；-------------begin
                        + ",    ' ' CUSTOMER_CODE "                                   
                        + ",    ' ' IOSTOCK_APPLY_NO "
                        + ",    ' ' IOSTOCK_APPLY_SUB_NO,' ' PACK_MODE_NAME  "
                        + ",    ROUND(MAT_WT / MAT_NUM, 3)  UNIT_WT_C "//提单需要的信息；-------------end                                    
                        + " ,t.*   FROM TMM0001   t          "
                        + " WHERE STOCK_NO= '" + STOCK_NO + "'  "
                        + " AND LOT_NO ='" + PROD_LOT_NO + "'"
                        + " AND MAT_NUM  >=1  AND MAT_WT >0 "
                        + " AND rownum = 1  "
                        + " AND (PROD_CODE,SG_SIGN,MAT_SPEC_DESC,PACK_MODE_CODE )    "
                        + " in (select PROD_CODE,SG_SIGN,ORD_SPEC_DESC,PACK_MODE_CODE  "
                        //upd 2025-08-15 一个提单有多个子项可能有问题（该物料可能一直是某个子项的包装方式）
                        // + "  from  TSOSD03  where BILL_OF_LADING_NO = '" + BILL_OF_LADING_NO + "') "
                        + "  from  TSOSD03  where '" + BILL_OF_LADING_SUB_NO + "' like '%'||BILL_OF_LADING_SUB_NO||'%'   ) "
                        ;

           


            Block_TMM0001 = EF.Utility.ExecQuery(sql_temp);
            if (Block_TMM0001.Tables[0].Rows.Count < 1)
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "该仓库下没有满足该提单[" + BILL_OF_LADING_NO + "]的库存！");
                return outBlock;
            }
            else
            {
                
                Block_TMM0001.Tables[0].Rows[0]["BILL_OF_LADING_SUB_NO"] = BILL_OF_LADING_SUB_NO; //便于前台快速比对；
                Block_TMM0001.Tables[0].Rows[0]["PONO_NO"] = MAT_NO; // 

                //2025-06-14 添加，若ACT_WT有值，则地磅不显示；---------------------------
                Block_TMM0001.Tables[0].Rows[0]["ACT_WT"] = 0;
                //-------------------------------------------------------------------------

                Block_TMM0001.Tables[0].Rows[0]["MAT_NUM"] = 1;//
                if (Block_TMM0001.Tables[0].Rows.Count>0)
                {
                    Block_TMM0001.Tables[0].Rows[0]["MAT_WT"] = Block_TPMMM02N.Tables[0].Rows[0]["MAT_WT"];//


                    Block_TMM0001.Tables[0].Rows[0]["PACK_MODE_NAME"] = Block_TPMMM02N.Tables[0].Rows[0]["PACK_MODE_NAME"];//


                    
                }
                else
                {
                    decimal mat_wt = decimal.Parse(Block_TMM0001.Tables[0].Rows[0]["MAT_WT"].ToString()) / decimal.Parse(Block_TMM0001.Tables[0].Rows[0]["MAT_NUM"].ToString());
                    mat_wt = Math.Round(mat_wt, 3);

                    Block_TMM0001.Tables[0].Rows[0]["MAT_WT"] = mat_wt;
                }
               


            }



            ////满足的提单系列，暂时不传前台；
            //Block_TSOSD03.Tables[0].TableName = "TSOSD03";
            //outBlock.Tables.Add(Block_TSOSD03.Tables[0].Copy());

            //可以的材料号，MAT_NUM [MAX_NUM 不校验，一直在变动，有销售出库确认时再校验]
            Block_TMM0001.Tables[0].TableName = "TMM0001";
            outBlock.Tables.Add(Block_TMM0001.Tables[0].Copy());

            outBlock.Tables["SYS"].Rows.Add(0, "");

            return outBlock;

        }



        //画面初始化【主要是各类小代码，类似之前画面的初始化参数】，暂时未用；
        //[HttpPost]
        //[Route("PostYm01xs_Load")]
        //public System.Data.DataSet PostYm01xs_Load([FromBody]  JObject obj)
        //{
        //    //系统信息；
        //    EI.EIInfo outBlock = new EI.EIInfo();
        //    outBlock.Tables.Add("SYS");
        //    outBlock.Tables["SYS"].Columns.Add("FLAG");
        //    outBlock.Tables["SYS"].Columns.Add("MSG");


        //    outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
        //    return outBlock;


        //}


        //销售出库保存
        [HttpPost]
        [Route("PostYm01xs_save")]
        public System.Data.DataSet PostYm01xs_save([FromBody]  JObject obj)
        {

            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");

            

            // MAT_NO，PROD_LOT_NO 必须有一个值；钉钉前端校验；
            string BILL_OF_LADING_NO = obj["BILL_OF_LADING_NO"] == null ? "" : obj["BILL_OF_LADING_NO"].ToString();
            string STOCK_NO = obj["STOCK_NO"] == null ? "" : obj["STOCK_NO"].ToString();
            string VEHICLE_NO = obj["VEHICLE_NO"] == null ? "" : obj["VEHICLE_NO"].ToString();
            string ORD_CUST_CODE = obj["ORD_CUST_CODE"] == null ? "" : obj["ORD_CUST_CODE"].ToString();


            

            string OUT_STOCK_NO = obj["OUT_STOCK_NO"] == null ? "" : obj["OUT_STOCK_NO"].ToString();


            string USERID = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string USERNAME = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();
            string PRO_TYPE = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString();


            DataTable table_rec = JsonConvert.DeserializeObject<DataTable>(obj["table_rec"].ToString(), new DingApi.ReadAheadDataTableConverter());;





            EI.EIInfo inBlock = new EI.EIInfo();

       



            /*1
             * 主信息
             * 前台43
               inBlock.SetBlkName(1, "TMM0022");
               inBlock.Tables[0].Merge(this.dataSetMM0202.TMM0022);
             * 
             * 关键字段处理如下：             
             */
            inBlock.SetBlkName(1, "TMM0022");
            inBlock.Tables["TMM0022"].Columns.Add("OUT_STOCK_APPLY_NO");
            inBlock.Tables["TMM0022"].Columns.Add("OUT_STOCK_MODE");
            inBlock.Tables["TMM0022"].Columns.Add("BILL_STATUS");
            inBlock.Tables["TMM0022"].Columns.Add("REC_CREATOR");

            inBlock.Tables["TMM0022"].Columns.Add("OUT_STOCK_DATE");
            inBlock.Tables["TMM0022"].Columns.Add("OUT_STOCK_MAKER");
            inBlock.Tables["TMM0022"].Columns.Add("IN_STOCK_STOCK_NO");
            inBlock.Tables["TMM0022"].Columns.Add("COMPANY_CODE");
            inBlock.Tables["TMM0022"].Columns.Add("BRANCH_CODE");
            inBlock.Tables["TMM0022"].Columns.Add("STOCK_CHAR");
          
            
            inBlock.Tables["TMM0022"].Columns.Add("VEHICLE_NO");

            inBlock.Tables["TMM0022"].Columns.Add("OUT_STOCK_NO");
            inBlock.Tables["TMM0022"].Columns.Add("CUSTOMER_CODE");


            


            //STOCK_CHAR,CUSTOMER_CODE,

            inBlock.Tables["TMM0022"].Rows.Add();
            inBlock.Tables["TMM0022"].Rows[0]["OUT_STOCK_APPLY_NO"] = BILL_OF_LADING_NO;
            inBlock.Tables["TMM0022"].Rows[0]["OUT_STOCK_MODE"] = "XC";//销售出库
            inBlock.Tables["TMM0022"].Rows[0]["BILL_STATUS"] = "10";
            inBlock.Tables["TMM0022"].Rows[0]["REC_CREATOR"] = USERID;




            inBlock.Tables["TMM0022"].Rows[0]["OUT_STOCK_DATE"] = DateTime.Now.ToString("yyyyMMddHHmmss");
            inBlock.Tables["TMM0022"].Rows[0]["OUT_STOCK_MAKER"] = USERID;
            inBlock.Tables["TMM0022"].Rows[0]["IN_STOCK_STOCK_NO"] = STOCK_NO;
            inBlock.Tables["TMM0022"].Rows[0]["COMPANY_CODE"] = "NM";
            inBlock.Tables["TMM0022"].Rows[0]["BRANCH_CODE"] = "N";
            inBlock.Tables["TMM0022"].Rows[0]["VEHICLE_NO"] = VEHICLE_NO;
            inBlock.Tables["TMM0022"].Rows[0]["OUT_STOCK_NO"] = OUT_STOCK_NO;            
            inBlock.Tables["TMM0022"].Rows[0]["CUSTOMER_CODE"] = ORD_CUST_CODE;

         




            string sql_strA1 = "   SELECT   STOCK_CHAR  FROM  TSIMM01  ";
            sql_strA1 += " WHERE  STOCK_NO='" + STOCK_NO + "'";
            EI.EIInfo BInfo_A1 = EF.Utility.ExecQuery(sql_strA1);

            string stock_char = BInfo_A1.Tables[0].Rows[0]["STOCK_CHAR"].ToString();
            inBlock.Tables["TMM0022"].Rows[0]["STOCK_CHAR"] = stock_char;




            inBlock.Tables.Add("TMM0212_ADD");
            inBlock.Tables["TMM0212_ADD"].Merge(table_rec);



            //第二次保存，删除所有子项重新提交；
            if (OUT_STOCK_NO.Trim() != "")
            {
                string str_del = " select *   from  tmm0011   where   IOSTOCK_BILL_NO =  '" + OUT_STOCK_NO + "' ";

                EI.EIInfo inblock_del = EF.Utility.ExecQuery(str_del);
                if (inblock_del.Tables[0].Rows.Count > 0)
                {
                    inblock_del.Tables[0].TableName = "TMM0212_DELETE";

                    //新增删除子表
                    inBlock.Tables.Add(inblock_del.Tables[0].Copy());

                }
            }


            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.company_name = "常熟市龙腾特种钢有限公司";

            inBlock.sys_info.userid = USERID;
            inBlock.sys_info.username = USERNAME;
            inBlock.sys_info.formname = FORMNAME;






            // 调用后台services保存信息
            EI.EIInfo  outBlock2 = EI.EITuxedo.CallService("mm0050_save", inBlock);
            if (outBlock2.sys_info.flag != 0)
            {
                outBlock.Tables["SYS"].Rows.Add(outBlock2.sys_info.flag, outBlock2.sys_info.msg);
                return outBlock;
            }
            else
            {
                outBlock2.Tables[0].TableName = "Table0";
                outBlock.Tables.Add(outBlock2.Tables[0].Copy()); 
                outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, "销售出库成功!出库单号[" + outBlock2.DataSet.Tables[0].Rows[0]["OUT_STOCK_NO"].ToString() + "]");
                return outBlock;
            }


        }


        //销售出库确认PA31(19->31);确认回退PB31（31->19);DEL
        [HttpPost]
        [Route("PostYm01xs_pro")]
        public System.Data.DataSet PostYm01xs_pro([FromBody]  JObject obj)
        {

            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");




            string out_stock_no = obj["OUT_STOCK_NO"]   == null ? ""    : obj["OUT_STOCK_NO"].ToString();
            string pro_type     = obj["PRO_TYPE"]       == null ? " "   : obj["PRO_TYPE"].ToString();






            string USERID   = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string USERNAME = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();




            EI.EIInfo inblock = new EI.EIInfo();


            inblock.Tables[0].Columns.Add("OUT_STOCK_NO");
            inblock.Tables[0].Rows.Add(out_stock_no);





            inblock.sys_info.company_code = "LT";
            inblock.sys_info.company_name = "常熟市龙腾特种钢有限公司";

            inblock.sys_info.userid = USERID;
            inblock.sys_info.username = USERNAME;
            inblock.sys_info.formname = FORMNAME;


            //
            inblock.Tables[0].TableName = pro_type;

            string svr = "pmmm02n_pro";

            if (pro_type.ToUpper() == "DEL") //
            {
                svr = "mm0020_del";
            }


            // 调用后台services保存信息 //调拨出库；
            EI.EIInfo outBlock2 = EI.EITuxedo.CallService(svr, inblock);
            if (outBlock2.sys_info.flag != 0)
            {
                outBlock.Tables["SYS"].Rows.Add(outBlock2.sys_info.flag, outBlock2.sys_info.msg);
                return outBlock;
            }
            else
            {
                outBlock.Tables["SYS"].Rows.Add(outBlock2.sys_info.flag, outBlock2.sys_info.msg);
                return outBlock;
            }


        }





        [HttpPost]
        [Route("PostPmmm02n_pro_all")]

        //多记录操作：入库申请撤销 PA14->PA11；入库申请撤销PA11；入库确认PA19；
        public System.Data.DataSet PostPmmm02n_pro_all([FromBody]  JObject obj)
        {
            string USERID = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string USERNAME = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();
            string SRV = obj["SRV"] == null ? " " : obj["SRV"].ToString();



            DataTable table_rec = JsonConvert.DeserializeObject<DataTable>(obj["table_rec"].ToString(), new DingApi.ReadAheadDataTableConverter());;

            EI.EIInfo inBlock = new EI.EIInfo();
            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.company_name = "常熟市龙腾特种钢有限公司";

            inBlock.sys_info.userid = USERID;
            inBlock.sys_info.username = USERNAME;
            inBlock.sys_info.formname = FORMNAME;

            inBlock.Tables[0].Merge(table_rec);

            //PA11 入库申请回退（默认值）
            inBlock.Tables[0].TableName = "PA11";

            string svr_name = "";
            //修改或删除操作；
            if (SRV.Trim().Contains("PA") || SRV.Trim().Contains("PB"))
            {
                svr_name = "pmmm02n_pro";
                inBlock.Tables[0].TableName = SRV.Trim().ToUpper();
            }
            else
            {
                svr_name = SRV;
            }

            EI.EIInfo outBlock = EI.EITuxedo.CallService(svr_name, inBlock);


            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");

            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }


        [HttpPost]
        [Route("PostPmmm02n_save2")]

        //库位变更、类别变更
        public System.Data.DataSet PostPmmm02n_save2([FromBody]  JObject obj)
        {


            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            EI.EIInfo inBlock = new EI.EIInfo();


             

            inBlock.Tables[0].Columns.Add("COMPANY_CODE");
            inBlock.Tables[0].Columns.Add("ASMAT_NO");
            inBlock.Tables[0].Columns.Add("STOCK_NO");
            inBlock.Tables[0].Columns.Add("ASMAT_BIG_TYPE");
            inBlock.Tables[0].Columns.Add("ASMAT_BIG_NAME");
            inBlock.Tables[0].Columns.Add("ASMAT_SMALL_TYPE");
            inBlock.Tables[0].Columns.Add("ASMAT_SMALL_NAME");
            inBlock.Tables[0].Columns.Add("PA_LOCATION_NO");
            inBlock.Tables[0].Columns.Add("REMARK");
            inBlock.Tables[0].Columns.Add("USE_PLACE");
            inBlock.Tables[0].Columns.Add("DPTMENT_CODE");


            inBlock.Tables[0].Columns.Add("COMPANY_CNAME");

           
          

            inBlock.Tables[0].Rows.Add();
            inBlock.Tables[0].Rows[0]["COMPANY_CODE"] = obj["COMPANY_CODE"] == null ? " " : obj["COMPANY_CODE"].ToString();
            inBlock.Tables[0].Rows[0]["ASMAT_NO"] = obj["ASMAT_NO"] == null ? " " : obj["ASMAT_NO"].ToString();
            inBlock.Tables[0].Rows[0]["STOCK_NO"] = obj["STOCK_NO"] == null ? " " : obj["STOCK_NO"].ToString();

            inBlock.Tables[0].Rows[0]["ASMAT_BIG_TYPE"] = obj["ASMAT_BIG_TYPE"] == null ? " " : obj["ASMAT_BIG_TYPE"].ToString();
            inBlock.Tables[0].Rows[0]["ASMAT_BIG_NAME"] = obj["ASMAT_BIG_NAME"] == null ? " " : obj["ASMAT_BIG_NAME"].ToString();
            inBlock.Tables[0].Rows[0]["ASMAT_SMALL_TYPE"] = obj["ASMAT_SMALL_TYPE"] == null ? " " : obj["ASMAT_SMALL_TYPE"].ToString();
            inBlock.Tables[0].Rows[0]["ASMAT_SMALL_NAME"] = obj["ASMAT_SMALL_NAME"] == null ? " " : obj["ASMAT_SMALL_NAME"].ToString();
            inBlock.Tables[0].Rows[0]["PA_LOCATION_NO"] = obj["PA_LOCATION_NO"] == null ? " " : obj["PA_LOCATION_NO"].ToString();
            inBlock.Tables[0].Rows[0]["REMARK"] = obj["REMARK"] == null ? " " : obj["REMARK"].ToString();
            inBlock.Tables[0].Rows[0]["USE_PLACE"] = obj["USE_PLACE"] == null ? " " : obj["USE_PLACE"].ToString();
            inBlock.Tables[0].Rows[0]["DPTMENT_CODE"] = obj["DPTMENT_CODE"] == null ? " " : obj["DPTMENT_CODE"].ToString();


            //修改之前的类别           
            inBlock.Tables[0].Rows[0]["COMPANY_CNAME"] = obj["COMPANY_CNAME"] == null ? " " : obj["COMPANY_CNAME"].ToString();


    



            string srv = obj["SRV"] == null ? " " : obj["SRV"].ToString();
          

            //块0为删除；
            //块1为新增
            //块2为修改
            EI.EIInfo inBlock2 = new EI.EIInfo();
            inBlock2.sys_info.company_code = "LT";
            inBlock2.sys_info.company_name = "龙腾特钢";

            inBlock2.sys_info.userid = obj["USERID"].ToString();
            inBlock2.sys_info.username = obj["USERNAME"].ToString();
            inBlock2.sys_info.formname = obj["FORMNAME"].ToString();


            //inBlock2.Tables.Add();
            //inBlock2.Tables.Add();

            //库位变更
            if (srv.Contains("pabmb1_save"))
            {
                //inBlock2.Tables.Add(inBlock.Tables[0].Copy());
                inBlock2.Tables[0].Merge(inBlock.Tables[0].Copy());

                inBlock2.Tables[0].TableName = "INS_UPD";


            }

            //类别变更
            else if (srv.Contains("pa0006_save"))
            {
                //inBlock2.Tables.Add(inBlock.Tables[0].Copy());
                inBlock2.Tables[0].Merge(inBlock.Tables[0].Copy());
                inBlock2.Tables[0].TableName = "UPD_TYPE";
            }

            else if (srv.Contains("pa0006_upd1"))
            {
                //inBlock2.Tables.Add(inBlock.Tables[0].Copy());
                inBlock2.Tables[0].Merge(inBlock.Tables[0].Copy());
                inBlock2.Tables[0].TableName = "UPD1";
                srv = "pa0006_save";
            }

          



            //STOCK_NO,ASMAT_NO,ASMAT_BIG_TYPE,ASMAT_SMALL_TYPE,ASMAT_BIG_NAME,ASMAT_SMALL_NAME
            EI.EIInfo outBlock = EI.EITuxedo.CallService(srv, inBlock2);


            //srv = "dcjl42_pass";
            //srv = "dcjl42_reback";


          






            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;

        }

    }
}